package groovy.jmx.builder

import java.lang.management.ManagementFactory
import javax.management.MBeanServer
import javax.management.MBeanServerFactory




class JmxMBeanExportFactory extends AbstractFactory {
    def server

    public Object newInstance(FactoryBuilderSupport builder, Object nodeName, Object nodeArgs, Map nodeAttribs) {
        server = nodeArgs
        if (!server || !server instanceof MBeanServer) {
            def servers = MBeanServerFactory.findMBeanServer(null);
            server = servers.size() > 0 ? servers[0] : ManagementFactory.getPlatformMBeanServer()
        }

        return server
    }

    public boolean onHandleNodeAttributes(FactoryBuilderSupport builder, Object node, Map nodeAttribs) {
        return true;
    }

    public boolean isLeaf() {
        return false
    }

}